<!--
 * @FileDescription:
 * @Author: cjw
 * @Date: 2022/3/12 22:47
 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :destroy-on-close="close_modal_destroy"
    :close-on-click-modal="close_modal"
    @close="handleCancel"
    width="500px"
  >
    <el-form ref="addFrom" :model="addFrom" label-width="120px" :rules="rules">
      <el-form-item label="资源名称" prop="name" required>
        <el-input v-model="addFrom.name"></el-input>
      </el-form-item>
      <el-form-item label="访问路由" prop="url" required>
        <el-input v-model="addFrom.url"></el-input>
      </el-form-item>
      <el-form-item label="访问组件路径" prop="src" required>
        <el-input v-model="addFrom.src"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon" required>
        <el-input v-model="addFrom.icon"></el-input>
      </el-form-item>
      <el-form-item label="父节点" prop="parentId">
        <el-input v-model="addFrom.parentId"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addFrom')">立即发布</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>

import {add, detail, update} from "@/api/resource";

export default {
  name: "addoreditMenuPermission",
  data() {
    return {
      title: undefined,
      visible: false,
      close_modal_destroy: true,
      close_modal: false,
      addFrom: {
        name: undefined,
        url: undefined,
        src: undefined,
        icon: undefined,
        parentId: undefined
      },
      rules: {
        name: [
          {required: true, message: '请输入资源名称', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '请输入访问路由', trigger: 'blur'}
        ],
        src: [
          {required: true, message: '请输入访问组件路径', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '请输入图标', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    close() {
      this.visible = false
      this.title = undefined
      this.addFrom = {
        name: undefined,
        url: undefined,
        src: undefined,
        icon: undefined,
        parentId: undefined
      }
    },
    handleCancel() {
      this.close()
    },
    add() {
      this.title = '新增菜单'
      this.visible = true
    },
    edit(id) {
      this.title = '修改菜单'
      this.visible = true
      this.addFrom.id = id
      detail(id).then(res => {
        this.addFrom = res.body.data
      })
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if(valid) {
          if (this.title === '新增菜单') {
            add(this.addFrom).then(res => {
              if (res.body.data) {
                this.$message.success("新增成功")
                this.$emit('ok')
                this.close()
              } else {
                this.$message.error("请重新操作")
              }
            })
          } else {
            update(this.addFrom).then(res => {
              if (res.body.data) {
                this.$message.success("修改成功")
                this.$emit('ok')
                this.close()
              } else {
                this.$message.error("请重新操作")
              }
            })
          }
        }
        else {
          this.$message.error("请输入必选项")
          return false
        }
      })

    }
  }
}
</script>

<style scoped>

</style>
